# include "student.h"

Student * newStudent () {
	Student *new = (Student *) malloc(sizeof(Student));
	if (!new) logMessage("data","student","newStudent","Memória Insuficiente",CRITICAL);
	new->name = NULL;
	new->number = -1;
	new->type = -1;
	new->average = -1;
	new->selections = NULL;
	new->ucesWanted = 2;
	return new;
}

Selection * newSelection (char *uceName, int pref) {
	Selection *new = (Selection *) malloc(sizeof(Selection));
	if (!new) logMessage("data","student","newSelection","Memória Insuficiente",CRITICAL);
	new->uceName = uceName;
	new->preference = pref;
	return new;
}

void freeSelection (Selection *sel) {
	if (sel) {
		if (sel->uceName) free(sel->uceName);
		free(sel);
	}
}

void freeStudent (Student *st) {
	LinkedList *aux;
	Selection *sel;
	if (st) {
		if (st->name) free(st->name);
		aux = st->selections;
		while (aux) {
			sel = getData(aux);
			freeSelection(sel);
			aux = aux->next;
		}
		freeLinkedList(st->selections);
		free(st);
	}
}
int compareStudent (Student *stA, Student *stB) {
	if (stA->average > stB->average) return 1;
	else if (stA->average < stB->average) return -1;
	else {
		if (stA->year < stB->year) return 1;
		else if (stA->year > stB->year) return -1;
		else {
			return 1; // Grande sorte do 1º!
		}
	}
}
